Uec3.PartnerQueryWindow = Ext.extend(Ext.app.Module,{
    id: 'partner_query',
    type:'common/partnerQuery',   
    
    selectPartner : function(id){
    	var win = this.curWindow;
    	var store = win.get(0).get(1).getStore();
    	var record = store.getById(id);   						   	   
		var lookup = win.lookup;
		var oldValue = lookup.getValue();
		if(lookup.lookupCallBackEvents) {
	   		this.setLookupValue(record.data, win);
	   		lookup.fireEvent('lookupChange', record);
        }else{
       		lookup.fireEvent('lookupChange', record, this, oldValue, win);
        }
        win.close();
    },
    
    getPanel:function(param){
       
    	var module = this;
    	
    	var partnerQuery_store = new Ext.data.JsonStore({
    	    url: Uec3.App.Constants.homeUrl+'/partner/query_partner!queryPartner.do',
    	    totalProperty: 'totalProperty',
			root: 'root',
			fields:['id','name','taxCode','code','pyCode','companyId','contactor','relation','address'],
			listeners :{
              beforeload:function(){
                 Ext.MessageBox.wait("正在查询客户信息...","提示");
              }
    	    }
    	});
    	
    	var partner_relation = new Ext.data.JsonStore({
    	    url:Uec3.App.Constants.homeUrl+'/dictionary/findByVarGroup.do?group=partner_relation',
    	    root:'',
			fields:[{name:'varIndex'},{name:'varName'}]
    	});
    	
    	var partner_comboRelation=new Ext.form.ComboBox ({										
			hiddenName:'relation',		
			fieldLabel:'往来关系',
			store: partner_relation,
			forceSelection: true, 
		    emptyText:'--请选择--',
			mode:'local',
			triggerAction:'all',
			valueField:'varIndex',
			displayField:'varName',
			loadingText : '加载中...',
			editable: false,
			allowBlank:false,
			blankText:"往来关系不能为空"										
		})
    	
    	/*var partnerQuery_selectModel = new Ext.grid.CheckboxSelectionModel({
    	    checkOnly : true,
    	    moveEditorOnEnter : true,
    	    singleSelect : true
    	});*/
    	     
    	var partnerQuery_column = new Ext.grid.ColumnModel({
    	    columns:[
    	      {
				header : "操作",
				width : 35,
				dataIndex : 'id',
           		renderer:function(value,meta,record,rowIndex,colIndex,store){
            		var str="<a style='font-size:12px' href='javascript:Ext.CUR_APP.getModule(\""+module.id+"\").selectPartner("+record.id+");'>选中</a>"
            	  	return str;}
    	      },
    	      {header:'公司名称',dataIndex:'name',width:130},
    	      {header:'税号',dataIndex:'taxCode'},
    	      {header:'机构代码',dataIndex:'code'},
    	      {header:'联系人',dataIndex:'contactor'},
    	      {header:'往来关系',dataIndex:'relation',renderer:Ext.util.Format.comboRenderer(partner_comboRelation)},
    	      {header:'通讯地址',dataIndex:'address',width:210}
    	    ]
    	});
    	
    	
    	var partnerQuery_grid = new Ext.grid.GridPanel({
    	     height:410, 
    	     enableHdMenu : false,
	   	     autoScroll : true,
	   	     titleCollapse:true,	   	    
	         colModel:partnerQuery_column,	     
	         store:partnerQuery_store,	           
	         columnLines :true,
	         stripeRows :true,
	         bbar:new Ext.PagingToolbar({		        	
		            pageSize:8,
	                store:partnerQuery_store,
	                displayInfo: true,
	                displayMsg: '第 {0} - {1} 条  共 {2} 条',
                    emptyMsg: "没有记录 ",                    
                    listeners:{
                       beforechange:function(){
                    		 Ext.MessageBox.wait("正在查询客户信息...","提示");	                     
                       },
                       change:function(){
                           Ext.MessageBox.hide();
                       }
                    }
		     }),
		     listeners : {
				   rowdblclick:function(grid,rowIndex,e){
				   	   var record = grid.getStore().getAt(rowIndex);
				   	   //alert(Ext.encode(record.data));
				   	   var win = param.desktopModule.curWindow;	
				   	   
				       var lookup = win.lookup;
				   	   var oldValue = lookup.getValue();
				   	   
				   	   
				   	   
//				   	   alert(oldValue);
//				   	   var value = {'name':record.data.name,'pyCode':record.data.pyCode,'id':record.data.id};
				      
				   	   
				       /*if(lookup.lookupCallBackEvents) {
				       	    if(param.purchase_item_store){
				       	    	var count = param.purchase_item_store.getCount();
				       	    }			       		
					       	Ext.each(lookup.lookupCallBackEvents, function(event) {
					       		if(event == 'lookupChange' || event == 'change') {
//					       			alert(record.data.id);
					       			if((oldValue + '').trim() != '' && record.data.id != oldValue ) {
					       				if(count > 0) {
						       				Ext.Msg.confirm("提示", '更改供货单位将删除原有零件记录，您确定修改吗？', function(btnId) {
						       					if(btnId == 'no') {
													return;
												} else {
//						       						alert(btnId);
											   	    module.setLookupValue(record.data, win);
									       			lookup.fireEvent(event, record);
												}
									       	});
					       				} else {
						       				module.setLookupValue(record.data, win);
									       	lookup.fireEvent(event, record);
					       				}
					       			} else {
					       				if(record.data.id != oldValue) {
					       					module.setLookupValue(record.data, win);
									       	lookup.fireEvent(event, record);
					       				}
					       			}
					       		} else {
					       			lookup.fireEvent(event);
					       		}
					       	});
				       }*/
				       if(lookup.lookupCallBackEvents) {
				   	   		module.setLookupValue(record.data, win);
				   	   		lookup.fireEvent('lookupChange', record);
				       }else{
				       		lookup.fireEvent('lookupChange', record, module, oldValue, win);
				       }
				     /*  module.setLookupValue(record.data, win);
				   	   		lookup.fireEvent('lookupChange', record, module, oldValue, win);*/
				       win.close();
				   }
				}
    	});
    	
    	var partnerQuery_panel = new Ext.FormPanel({
    	     border:true,
    	     items:
    	       [{
    	           height:60,
		           layout:'hbox',
		           layoutConfig: 
		             {     
		               padding:'5',
					   align:'middle' 
                     }, 
                   style:'padding:0px;fontSize:13px;',
		           items:[			             
		               {
					      xtype:'label',
					      text:'公司名称:',
					      style:{width:'62px'}
					    },{
					      xtype:'textfield',
					      name:'name',
					      style:{width:'100px'}				     
					    },{
					      xtype:'label',
					      text:'税号:',
					      style:{marginLeft:'5px',width:'37px'}
					    },{
					      xtype:'textfield',
					      name:'taxCode',
					      style:{width:'100px'}
					    },{
					      xtype:'label',
					      text:'机构代码:',
					      style:{marginLeft:'5px',width:'62px'}
					    },{
					      xtype:'textfield',
					      name:'code',
					      style:{width:'100px'}
					    },{
					      xtype:'label',
					      text:'公司拼音码:',
					      style:{marginLeft:'5px',width:'80px'}
					    },{
					      xtype:'textfield',
					      name:'pyCode',
					      style:{width:'100px'}
					    },{						      
					       xtype:'button',
					       text:'查询',
					       style:{marginLeft:'15px',width:'75px'},
					       handler:searchPartnerByCondition
					   }
		             ]
    	         },partnerQuery_grid
    	       ]
    	   });
    	   
    	   function searchPartnerByCondition(){
    	       var queryParam = partnerQuery_panel.getForm().getValues();
    	       loadPartnerInfo(queryParam);
    	   }
    	    
    	   function loadPartnerInfo(queryParam){
    	   	    var partnerType =param.partnerType;
    	   	    var partnerQualify = param.partnerQualify;   	   	    
			    if(queryParam){
			    	if(partnerQualify){
			    		//queryParam.qualify = partnerQualify;
			    		partnerQuery_store.baseParams = {start:0,limit:8,queryParam:Ext.encode(queryParam),type:partnerType,qualify:partnerQualify}
			    	}else{
					    partnerQuery_store.baseParams = {start:0,limit:8,queryParam:Ext.encode(queryParam),type:partnerType}; 
			    	}
			    }else{
			    	if(partnerQualify){
			    		//var queryParam = {};
			    		//queryParam.qualify = partnerQualify;
			    		partnerQuery_store.baseParams = {start:0,limit:8,type:partnerType,qualify:partnerQualify}
			    	}else{
					    partnerQuery_store.baseParams = {start:0,limit:8,type:partnerType}; 
			    	}			  
			    }
		        partnerQuery_store.load({callback:function(records,options,success){
	               if(success){
	              	   Ext.MessageBox.hide();
	                   var total=records.length;
	                   if(!total){
	                   	   partnerQuery_panel.ownerCt.close();
	                       Ext.MessageBox.alert("提示","未查询到符合条件的客户信息,请先建立客户信息!");
	                   }
		           }else{
		           	    partnerQuery_panel.ownerCt.close();
	                   Ext.MessageBox.alert("提示","数据加载异常");
	               }
		       }});
	      }
	      
	      init();
	      function init(){
	      	 partner_relation.load();
	         loadPartnerInfo();
	      }
    	  
	      return partnerQuery_panel;
        },
    
    createWindow:function(param){
    	var desktop = this.app.getDesktop();
    	var win = desktop.getWindow("partner_query");
    	if(!win){    		
    	    win = desktop.createWindow({
            id: "partner_query",   
            modal : true,
            title: '客户查询',
            width: 800,
            height: 500,
            minWidth:800,
            minHeight:500,
            autoScroll : false,
            iconCls: 'acc-icon',
            shim: false,
            modal:true,
            animCollapse: false,
            constrainHeader: true,
            maximizable: true,
            minimizable : false,
            items:[this.getPanel(param)]         
         });	
       }
       win.show();
/*
       win.on("show",function(){
          var view_panel = this.getPanel(param);    
	      win.add(view_panel);
          win.doLayout();               
       },this);*/
       
       win.on('resize',function(win,width,height){     	
      	  win.get(0).get(1).setHeight(height-92);
       });
    }
});